package com.example.community.dao;

import com.example.community.mapper.ComplaintMapper;
import com.example.community.mapper.ComtyMapper;
import com.example.community.mapper.FloorMapper;
import com.example.community.mapper.RepairMapper;
import com.example.community.model.Complaint;
import com.example.community.model.Comty;
import com.example.community.model.Floor;
import com.example.community.model.Repair;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 投诉管理表
 */
public class ComplaintDao {
    public Complaint fandById(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            return mapper.selectByPrimaryKey(id);
        }
    }

    public List<Complaint> fandAll(){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            return mapper.selectAll();
        }
    }
    public List<Complaint> fandAllStatic(){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            return mapper.selectAllStatic();
        }
    }

    public List<Complaint> fandAllByUsNo(Integer usno){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            return mapper.selectAllByUsNo(usno);
        }
    }

    public List<Complaint> fandAllByUsNoStatic(Integer usno){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            return mapper.selectAllByUsNoStatic(usno);
        }
    }

    public List<Complaint> fandLimit(Integer ys, Integer shuju){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            Integer yss =(ys - 1) * shuju;
            return mapper.selectLimit(yss,shuju);
        }
    }

    public Integer selectLimitCount(Integer pageSize){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            int count = mapper.CountComplaint();
            return (count + pageSize - 1)/pageSize;
        }
    }

    public Integer insert(Complaint complaint){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            int i = mapper.insert(complaint);
            session.commit();
            return i;
        }
    }

    public Integer update(Complaint complaint){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            int i = mapper.updateByPrimaryKey(complaint);
            session.commit();
            return i;
        }
    }

    public Integer delete(Integer id){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            int i = mapper.deleteByPrimaryKey(id);
            session.commit();
            return i;
        }
    }

    public List<Complaint> fandLimitByUsNo(Integer ys, Integer shuju, Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            Integer yss =(ys - 1) * shuju;
            return mapper.selectLimitByUsNo(yss,shuju,usNo);
        }
    }

    public Integer selectLimitCountByUsNo(Integer pageSize,Integer usNo){
        try (SqlSession session = MybatisUtil.getSession()){
            final ComplaintMapper mapper = session.getMapper(ComplaintMapper.class);
            int count = mapper.CountComplaintByUsNo(usNo);
            return (count + pageSize - 1)/pageSize;
        }
    }
}
